Code for the Car Rental System
Write object-oriented code to implement the design of the car rental system problem.
We’ve reviewed different aspects of the car rental system and observed the attributes attached to the problem using various UML diagrams. Let’s explore the more practical side of things, where we will work on implementing the car rental system using multiple languages. This is usually the last step in an object-oriented design interview process.
We have chosen the following languages to write the skeleton code of the different classes present in the car rental system:
Java
C#
Python
C++
JavaScript
Car rental system classes#
In this section, we will provide the skeleton code of the classes designed in the class diagram lesson.
Note: For simplicity, we are not defining getter and setter functions. The reader can assume that all class attributes are private and accessed through their respective public getter methods and modified only through their public methods function.
Enumerations#
First, we will define all the enumerations required in the car rental system. According to the class diagram, there are seven enumerations used in the system, i.e., VehicleStatus
, AccountStatus
, ReservationStatus
, PaymentStatus
, VanType
, CarType
, and VehicleLogType
. The code to implement these enumerations is as follows:
Note: JavaScript does not support enumerations, so we will use the
Object.freeze()
method as an alternative that freezes an object and prevents further modifications.
Address, person, and driver#
This section contains the Address
, Person
, and Driver
classes, where the first two classes are used as a custom data type. The implementation of these classes is shown below:se classes can be found below:
Account#
Account
is an abstract class that represents the various people or actors that can interact with the system. There are two types of accounts: receptionist and customer. The implementation of Account
and its subclasses is shown below:
Vehicle#
Vehicle
will be another abstract class, which serves as a parent for four different types of vehicles: Car
, Van
, Truck
, and MotorCycle
. The definition of the Vehicle
and its child classes is given below:
Equipment#
Equipment
is an abstract class, and this section represents different equipment: Navigation
, ChildSeat
, and SkiRack
added in the reservation. The code to implement these classes is shown below:
Service#
Service
is an abstract class, and this section represents different services: DriverService
, RoadsideAssistance
, and Wi-Fi
added to the reservation. The code to implement these classes is shown below:
Payment#
The Payment
class is another abstract class, with the Cash
and CreditCard
classes as its child. This takes in the PaymentStatus
enum to keep track of the payment status. The definition of this class is provided below:
Vehicle log and Vehicle reservation#
VehicleLog
is a class responsible for keeping track of all the events related to a vehicle. VehicleReservation
is a class responsible for managing the reservation of vehicles. The implementation of this class is given below:
Notification#
The Notification
class is another abstract class responsible for sending notifications, with the SMSNotification
and EmailNotification
classes as its child. The implementation of this class is shown below:
Parking stall and fine#
ParkingStall
is a class used to locate vehicles in the car rental branch while the Fine
class represents the fine applied on payment. The implementation of these classes is given below:
Search interface and vehicle catalog#
Search
is an interface and the VehicleCatalog
class is used to implement the search interface to help in vehicle searching. The code to perform this function is presented below:
Car rental system and car rental branch#
The CarRentalSystem
class is the base class of the system that is used to represent the whole car rental system (or the top-level classes of the system). CarRentalBranch
represents the single branch of the system. The implementation of these classes is given below:
Wrapping up#
We've explored the complete design of a car rental system in this chapter. We've looked at how a basic car rental system can be visualized using various UML diagrams and designed using object-oriented principles and design patterns.
Activity Diagram for the Car Rental System
Getting Ready: The ATM System